package com.fubang.greendaolearning;

import java.text.DateFormat;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.greenrobot.greendao.query.Query;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.et)
    EditText mEt;
    @BindView(R.id.bt)
    Button mBt;
    @BindView(R.id.recycler)
    RecyclerView mRecycler;
    private NoteDao noteDao;
    private Query<Note> noteQuery;
    private NoteAdapter noteAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        setUpViews();

        //获取dao
        DaoSession daoSession = ((GlobalApplication) getApplication()).getDaoSession();
        noteDao = daoSession.getNoteDao();
        //初始化查询
        noteQuery = noteDao.queryBuilder().orderAsc(NoteDao.Properties.Id).build();
        updateQueryNotes();


    }

    private void setUpViews() {
        mBt.setClickable(false);
        mEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    addNote();
                    return true;
                }
                return false;
            }
        });
        mEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                mBt.setClickable(true);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        noteAdapter = new NoteAdapter(listener);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecycler.setLayoutManager(linearLayoutManager);
        mRecycler.setAdapter(noteAdapter);
        mBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addNote();
            }
        });
    }

    NoteAdapter.OnNoteClickListener listener =new NoteAdapter.OnNoteClickListener() {
        @Override
        public void onNoteClick(int position) {
            Note note = noteAdapter.getNote(position);
            noteDao.deleteByKey(note.getId());
            updateQueryNotes();
        }
    };

    /**
     * 添加一条Note,添加完成之后，要把EditText置空
     */
    private void addNote() {
        String noteText = mEt.getText().toString();
        mEt.setText("");

        DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
        String comment = "添加于" + format.format(new Date());
        Note note = new Note();
        note.setText(noteText);
        note.setComment(comment);
        note.setDate(new Date());
        note.setType("1");
        noteDao.insert(note);
        updateQueryNotes();
    }

    private void updateQueryNotes() {
        List<Note> list = noteQuery.list();
        noteAdapter.SetNote(list);
    }
}
